package android_fields;

import static android_board.GUIBoard.FIELDHEIGHT;
import static android_board.GUIBoard.FIELDWIDTH;

import common.GUIColor;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android_board.GUIBoard;
import android_board.GUICenter;
import dk.dtu.compute.course02312.matadorfinal.MainActivity;
import dk.dtu.compute.course02312.matadorfinal.R;

public final class GUIStreet extends GUIOwnable {
	private static final float TITLEHEIGHTPART = 0.39f;
	// private static final float IMAGEHEIGHTPART = 0.39f;
	// private static final float SUBTEXTHEIGHTPART = 0.19f;
	private ImageView houseView;

	public static class Builder extends AndroidField.Builder<GUIStreet.Builder> implements
			iBuilder {
		public Builder() {
			this.title = "Street";
		}

		@Override
		@SuppressWarnings("synthetic-access")
		public final GUIStreet build() {
			return new GUIStreet(this.title, this.bgColor, this.fgColor,
					this.subText, this.description, this.rent);
		}

		public final Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public final Builder setSubText(String subText) {
			this.subText = subText;
			return this;
		}

		public final Builder setDescription(String description) {
			this.description = description;
			return this;
		}

		public final Builder setRent(String rent) {
			this.rent = rent;
			return this;
		}
	}

	private GUIStreet(String title, GUIColor bgColor, GUIColor fgColor, String subText,
			String description, String rent) {
		super(bgColor, fgColor, title, subText, description, rent);

		this.titleView.setText(this.title);
		this.titleView.setGravity(Gravity.CENTER);
		int height1 = (int) (TITLEHEIGHTPART * FIELDHEIGHT);
		RelativeLayout.LayoutParams titleParams = new RelativeLayout.LayoutParams(
				FIELDWIDTH, height1);

		Bitmap image = BitmapFactory
				.decodeResource(MainActivity.context.getResources(),
						R.drawable.building_4house);
		// int height2 = (int) (IMAGEHEIGHTPART * FIELDHEIGHT);
		// RelativeLayout.LayoutParams houseParams = new
		// RelativeLayout.LayoutParams(
		// FIELDWIDTH, height2);
		RelativeLayout.LayoutParams houseParams = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		houseParams.addRule(RelativeLayout.BELOW, this.titleView.getId());
		houseParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
		this.houseView = new ImageView(MainActivity.context);
		this.houseView.setId(GUIBoard.generateViewId());
		this.houseView.setImageBitmap(image);
		this.houseView.setPadding(2, 0, 2, 0);
		this.houseView.setVisibility(View.INVISIBLE);

		this.subTextView.setText(this.subText);

		this.cell.addView(this.titleView, titleParams);
		this.cell.addView(this.houseView, houseParams);

	}

	public void setHouses(int houseCount) {
		Bitmap icon = null;
		switch (houseCount) {
		case 1:
			icon = BitmapFactory.decodeResource(
					MainActivity.context.getResources(),
					R.drawable.building_1house);
			this.houseView.setVisibility(View.VISIBLE);
			break;
		case 2:
			icon = BitmapFactory.decodeResource(
					MainActivity.context.getResources(),
					R.drawable.building_2house);
			this.houseView.setVisibility(View.VISIBLE);
			break;
		case 3:
			icon = BitmapFactory.decodeResource(
					MainActivity.context.getResources(),
					R.drawable.building_3house);
			this.houseView.setVisibility(View.VISIBLE);
			break;
		case 4:
			icon = BitmapFactory.decodeResource(
					MainActivity.context.getResources(),
					R.drawable.building_4house);
			this.houseView.setVisibility(View.VISIBLE);
			break;

		default:
			this.houseView.setVisibility(View.INVISIBLE);
			break;
		}
		this.houseView.setImageBitmap(icon);
	}

	public void setHotel(boolean hasHotel) {
		Bitmap icon = BitmapFactory.decodeResource(
				MainActivity.context.getResources(), R.drawable.building_hotel);
		if (hasHotel) {
			this.houseView.setImageBitmap(icon);
			this.houseView.setVisibility(View.VISIBLE);
		} else {
			this.houseView.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public void displayOnCenter() {
		super.displayOnCenter();
		GUICenter.textViews[1].setText(this.description);
		GUICenter.textViews[1].setVisibility(View.VISIBLE);
		GUICenter.textViews[2].setText(this.subText);
		GUICenter.textViews[2].setVisibility(View.VISIBLE);

		if (this.owner != null) {
			GUICenter.textViews[3].setText(this.prefix + this.owner.getName()
					+ this.postfix);
			GUICenter.textViews[3].setVisibility(View.VISIBLE);
			GUICenter.textViews[4].setText(getLeje());
			GUICenter.textViews[4].setVisibility(View.VISIBLE);
		}
		GUICenter.textViews[4].setCompoundDrawables(null, null, null,
				this.houseView.getDrawable());
		GUICenter.textViews[4].setVisibility(this.houseView.getVisibility());

		super.displayCarOnCenter();
	}
}
